package com.self.shortlink.admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.self.shortlink.admin.common.convention.result.Result;
import com.self.shortlink.admin.remote.dto.resp.ShortLinkStatsAccessRecordRespDTO;
import com.self.shortlink.admin.remote.dto.resp.ShortLinkStatsRespDTO;
import com.self.shortlink.admin.remote.ShortLinkActualRemoteService;
import com.self.shortlink.admin.remote.dto.req.ShortLinkGroupStatsAccessRecordReqDTO;
import com.self.shortlink.admin.remote.dto.req.ShortLinkGroupStatsReqDTO;
import com.self.shortlink.admin.remote.dto.req.ShortLinkStatsAccessRecordReqDTO;
import com.self.shortlink.admin.remote.dto.req.ShortLinkStatsReqDTO;
import com.self.shortlink.admin.service.StatsAggregationService;
import com.self.shortlink.admin.service.dto.AggregatedStatsRespDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 短链接监控控制层
 */
@RestController(value = "shortLinkStatsControllerByAdmin")
@RequiredArgsConstructor
public class ShortLinkStatsController {

    private final ShortLinkActualRemoteService shortLinkActualRemoteService;
    private final StatsAggregationService statsAggregationService;

    /**
     * 访问单个短链接指定时间内的监控数据
     */
    @GetMapping("/api/short-link/admin/v1/stats")
    public Result<ShortLinkStatsRespDTO> shortLinkStats(ShortLinkStatsReqDTO requestParam) {
//        return shortLinkRemoteService.oneShortLinkStats(requestParam);
        return shortLinkActualRemoteService.oneShortLinkStats(
                requestParam.getFullShortUrl(),
                requestParam.getGid(),
                requestParam.getEnableStatus(),
                requestParam.getStartDate(),
                requestParam.getEndDate());
    }

    /**
     * 访问单个短链接指定时间内的访问记录监控数据
     */
    @GetMapping("/api/short-link/admin/v1/stats/access-record")
    public Result<Page<ShortLinkStatsAccessRecordRespDTO>> shortLinkStatsAccessRecord(ShortLinkStatsAccessRecordReqDTO requestParam) {
//        return shortLinkRemoteService.shortLinkStatsAccessRecord(requestParam);
        return shortLinkActualRemoteService.shortLinkStatsAccessRecord(
                requestParam.getFullShortUrl(),
                requestParam.getGid(),
                requestParam.getStartDate(),
                requestParam.getEndDate(),
                requestParam.getEnableStatus(),
                requestParam.getCurrent(),
                requestParam.getSize());
    }

    /**
     * 访问分组短链接指定时间内的监控数据
     */
    @GetMapping("/api/short-link/admin/v1/stats/group")
    public Result<ShortLinkStatsRespDTO> groupShortLinkStats(ShortLinkGroupStatsReqDTO requestParam) {
//        return shortLinkRemoteService.groupShortLinkStats(requestParam);
        return shortLinkActualRemoteService.groupShortLinkStats(
                requestParam.getGid(),
                requestParam.getStartDate(),
                requestParam.getEndDate());
    }

    /**
     * 访问分组短链接指定时间内的访问记录监控数据
     */
    @GetMapping("/api/short-link/admin/v1/stats/access-record/group")
    public Result<Page<ShortLinkStatsAccessRecordRespDTO>> groupShortLinkStatsAccessRecord(ShortLinkGroupStatsAccessRecordReqDTO requestParam) {
//        return shortLinkRemoteService.groupShortLinkStatsAccessRecord(requestParam);
        return shortLinkActualRemoteService.groupShortLinkStatsAccessRecord(
                requestParam.getGid(),
                requestParam.getStartDate(),
                requestParam.getEndDate(),
                requestParam.getCurrent(),
                requestParam.getSize());
    }

    /**
     * 聚合统计：服务端并发查询，多块数据一次返回
     */
    @GetMapping("/api/short-link/admin/v1/stats/aggregate")
    public Result<AggregatedStatsRespDTO> aggregate(ShortLinkStatsAccessRecordReqDTO requestParam) {
        AggregatedStatsRespDTO data = statsAggregationService.aggregateForShortLink(
                requestParam.getFullShortUrl(),
                requestParam.getGid(),
                requestParam.getEnableStatus(),
                requestParam.getStartDate(),
                requestParam.getEndDate(),
                requestParam.getCurrent(),
                requestParam.getSize());
        return new Result<AggregatedStatsRespDTO>()
                .setCode(Result.SUCCESS_CODE)
                .setMessage("OK")
                .setData(data);
    }
}
